New Rule S6966: Awaitable method should be used#9101
New Rule S6966: Awaitable method should be used#9101zsolt-kolbay-sonarsource merged 33 commits intomasterfrom
Conversation
...ers/its/expected/CSharpLatest/S6966-NetCore31.MVC.ConfigurableRules-netcoreapp3.1.Views.json
Show resolved
Hide resolved
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore6-net6.0.json
Show resolved
Hide resolved
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore7-net7.0.json
Show resolved
Hide resolved
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore8-net8.0.json
Show resolved
Hide resolved
analyzers/its/expected/akka.net/S6966-Akka.Cluster.Sharding-netstandard2.0.json
Show resolved
Hide resolved
|
|
||
| namespace Microsoft.CodeAnalysis.Shared.Extensions; | ||
|
|
||
| [GeneratedCode("Copied from Roslyn", "ca66296efa86bd8078508fe7b38b91b415364f78")] |
There was a problem hiding this comment.
Note to myself:
Needs to be changed to [ExcludeFromCodeCoverage]
https://sonarsource.slack.com/archives/C06S7H06Z4Y/p1713354144963039?thread_ts=1713264344.421699&cid=C06S7H06Z4Y
| #if NET | ||
|
|
||
| [TestMethod] | ||
| public void UseAwaitableMethod_CS_Test() => |
There was a problem hiding this comment.
This snippet is for testing only and will be removed before merging
zsolt-kolbay-sonarsource
left a comment
There was a problem hiding this comment.
LGTM. Left a bunch of polishing comments, but nothing major.
|
|
||
| ReturnMethod(); // Noncompliant | ||
| _ = ReturnMethod(); // Noncompliant | ||
| this.ReturnMethod().ReturnMethod().ReturnMethod(); |
There was a problem hiding this comment.
Add the following test case:
await ReturnMethod().ReturnMethodAsync(); // Noncompliant
// ^^^^^^^^^^^^^^| public static C operator -(C c) => default(C); | ||
| public static C operator -(C c1, C c2) => default(C); | ||
| public static C operator !(C c) => default(C); | ||
| public static C operator ~(C c) => default(C); |
There was a problem hiding this comment.
The ~ operator is not used.
| using System; | ||
|
|
||
| var ms = new MemoryStream(); | ||
| ms.Dispose(); // Noncompliant {{Await DisposeAsync instead.}} |
There was a problem hiding this comment.
The rule can be extended to also raise a warning a using statement without the await keyword in an async context (or maybe as a new rule?).
using var ms1 = new MemoryStream(); // Noncompliant
await using var ms2 = new MemoryStream(); // CompliantWDYT?
There was a problem hiding this comment.
👍 That requires a different implementation. Please create a Rule Idea issue.
| return wellKnownExtensionMethodContainer; | ||
| } | ||
|
|
||
| private static IEnumerable<IMethodSymbol> GetMethodSymbolsInScope(string methodName, WellKnownExtensionMethodContainer wellKnownExtensionMethodContainer, |
There was a problem hiding this comment.
Move this and the following method below FindAwaitableAlternatives.
|
Other well known extension methods to look at: |
| if (awaitableRoot is { Parent: AwaitExpressionSyntax }) | ||
| { | ||
| return ImmutableArray<ISymbol>.Empty; // Invocation result is already awaited. | ||
| } | ||
| if (invocationExpression.EnclosingScope() is { } scope && !IsAsyncCodeBlock(scope)) | ||
| { | ||
| return ImmutableArray<ISymbol>.Empty; // Not in an async scope | ||
| } |
There was a problem hiding this comment.
The two conditions can be merged into a single if statement.
Co-authored-by: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com>
ab1fc63 to
3d79d9d
Compare
|
|
This reverts commit 02fcc9e.
|
Peach validation:
PerformanceThe rule is about 3 times as expensive as other rules, like |
|
Peach validation: 430 issues found. All are TP with the following exceptions:
Most of the violations originate from (in that order)
We also have found issues in razor files ( |




Fixes #9096